/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Purse
 *    
 *    PasswordLicense
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2010-11-19
 *    @version: 1.0
 *
 ******************************************************************************/

package com.tool.finances.lifestyle;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tool.finance.manager.IntentManager;
import com.tool.finance.manager.ShareperfrenceManager;
import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;
import com.waps.AppConnect;

/**
 * PasswordLicense of Purse.
 * 
 * @author zuochen
 * 
 */

public class PasswordLicense extends Activity {
	private Button btnConfirm;
	private Button btnCancel;
	private EditText edtPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final ShareperfrenceManager mShareperfrenceManager = new ShareperfrenceManager(
				PasswordLicense.this);
		AppConnect.getInstance(this);
		if (0 == mShareperfrenceManager.getPasswordSate()) {
			// alert a dialog to request user to put password.
			setContentView(R.layout.password_check);
			bundleView();
			checkPassowrd();
		} else {
			final IntentManager mIntentManager = new IntentManager(
					PasswordLicense.this);
			mIntentManager.gotoFinanceManager();
			finish();
		}

	}

	private void bundleView() {
		btnConfirm = (Button) findViewById(R.id.passwordConfirmButton);
		btnCancel = (Button) findViewById(R.id.passwordCancelButton);
		edtPassword = (EditText) findViewById(R.id.password_edit);
	}

	private void checkPassowrd() {
		btnConfirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final Cursor cursor = getContentResolver().query(
						AccountProvider.PASSWORD_URI,
						AccountType.Account.PASSWORDPROJECTION, null, null,
						null);
				if (cursor.moveToFirst()) {
					final int index = cursor
							.getColumnIndex(AccountType.Account.PASSWORDTYPE);
					final String passwordString = cursor.getString(index);

					if (passwordString.equalsIgnoreCase(edtPassword.getText()
							.toString())) {
						final IntentManager manager = new IntentManager(
								PasswordLicense.this);
						manager.gotoFinanceManager();
						finish();

					} else {
						Toast.makeText(PasswordLicense.this, "Password error",
								Toast.LENGTH_SHORT).show();
						return;
					}
				}
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

}
